import { asyncExecFunction } from "@/king-components/js/KingonPlus";

export const openSubScreen = (url?: string, local = false) => {
  // #ifdef APP
  if (local && url) url = plus.io.convertLocalFileSystemURL(url);
  if (!url) url = `${import.meta.env.VITE_H5_BASE_URL}#/pages/customerScreen/defult-index`;
  //  (x) if (!url) url = `/hybrid/html/defult-index.html`;
  console.log("openSubScreen _. url", url);
  try {
    asyncExecFunction(
      "HnXfjDubScreen",
      JSON.stringify({
        Url: url,
      })
    );
  } catch (error) {
    console.error(error);
    // uni.showToast({ title: error.message, icon: "none" });
  }
  // #endif
};

export const closeSubScreen = () => {
  // #ifdef APP
  try {
    asyncExecFunction("HnXfjDubScreenClose");
  } catch (error) {
    console.error(error);
    // uni.showToast({ title: error.message, icon: "none" });
  }
  // #endif
};

/**
 * 更具体简介的固定参数打开
 */
interface ModeMaps {
  "standby-mode": string;
  "defult-index": string;
}
interface SubParams {
  mode: keyof ModeMaps;
  money?: string | number;
  action?: "waite-input" | "please-pay" | "awite-pay";
  payType?: "card" | "cash" | "face" | "scan";
  detaileInfo?: {
    mealAllowance: number;
    memberDiscount: number;
  };
}
export const openSubScreenByParams = ({ mode, money, action, payType, detaileInfo }: SubParams) => {
  const modeMaps: ModeMaps = {
    // "standby-mode": '#/pages/standby-mode',
    "standby-mode": "?noInit=1#/pages/customerScreen/standby-mode",
    "defult-index": "?noInit=1#/pages/customerScreen/defult-index",
  };
  // 更新客户屏
  // (x) openSubScreen(
  //   `/hybrid/html/sub-screen/index.html${modeMaps[mode]}?money=${money}&action=${action}&payType=${payType}`
  // );
  openSubScreen(
    `${import.meta.env.VITE_H5_BASE_URL}${modeMaps[mode]}?money=${money}&action=${action}&payType=${
      payType || ""
    }&detaileInfo=${JSON.stringify(detaileInfo || {})}`
  );
};
